package com.edu.service.impl;

import com.aliyuncs.exceptions.ClientException;
import com.edu.constant.RedisConstant;
import com.edu.dao.*;
import com.edu.entity.PageResult;
import com.edu.entity.QueryPageBean;
import com.edu.pojo.*;
import com.edu.service.SetmealService;
import com.edu.utils.SMSUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private TSetmealMapper tSetmealMapper;
    @Autowired
    private TSetmealCheckgroupMapper tSetmealCheckgroupMapper;
    @Resource
    private JedisPool jedisPool;
    @Autowired
    private TCheckgroupMapper tCheckgroupMapper;

    @Autowired
    private TCheckgroupCheckitemMapper tCheckgroupCheckitemMapper;
    @Autowired
    private TCheckitemMapper tCheckitemMapper;

    @Value("${out_put_path}")
    private String outPutPath;// 从属性文件中读取目录

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    public void add(TSetmeal tSetmeal, Integer[] checkgroupIds) {
        tSetmealMapper.insertSelective(tSetmeal);
        Integer id = tSetmeal.getId();
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,tSetmeal.getImg());
        for (Integer checkgroupId : checkgroupIds) {
            TSetmealCheckgroupKey tSetmealCheckgroupKey = new TSetmealCheckgroupKey();
            tSetmealCheckgroupKey.setSetmealId(id);
            tSetmealCheckgroupKey.setCheckgroupId(checkgroupId);
            tSetmealCheckgroupMapper.insertSelective(tSetmealCheckgroupKey);
        }
        // 当添加套餐的时候生成静态页面
        generateMobileStaticHtml();
    }

    public void generateMobileStaticHtml(){
        List<TSetmeal> setmealList = this.findAll();
        generateMobileSetmealListHtml(setmealList);
        generateMoblieSetmealDetailHtml(setmealList);
    }

    public void generateMobileSetmealListHtml(List<TSetmeal> setmealList){
        Map<String , Object> dataMap = new HashMap<String ,Object>();
        dataMap.put("setmealList",setmealList);
        this.generateHtml("mobile_setmeal.ftl","m_setmeal.html",dataMap);
    }

    public void generateMoblieSetmealDetailHtml(List<TSetmeal> setmealList){
        for (TSetmeal tSetmeal : setmealList) {
            Map<String, Object> dataMap = new HashMap<String,Object>();
            dataMap.put("setmeal",findById(tSetmeal.getId()));
            this.generateHtml("mobile_setmeal_detail.ftl","setmeal_detail_"+tSetmeal.getId()+".html",dataMap);
        }
    }

    public void generateHtml(String templateName, String htmlPageName, Map map){
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        configuration.setClassicCompatible(true);
        Writer out = null;
        try {
            Template template = configuration.getTemplate(templateName);
            File docFtile = new File(outPutPath + "/" + htmlPageName);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFtile)));
            template.process(map,out);
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = tSetmealMapper.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<TSetmeal> findAll() {
        TSetmealExample tSetmealExample = new TSetmealExample();
        List<TSetmeal> tSetmeals = tSetmealMapper.selectByExample(tSetmealExample);
        return tSetmeals;
    }

    @Override
    public TSetmeal findById(Integer id) {
        // 根据套餐的id 查询套餐
        TSetmeal tSetmeal = tSetmealMapper.selectByPrimaryKey(id);
        // 根据套餐id 查询出所有的检查组
        TSetmealCheckgroupExample tSetmealCheckgroupExample = new TSetmealCheckgroupExample();
        tSetmealCheckgroupExample.createCriteria().andSetmealIdEqualTo(id);
        // 查询出中间表
        List<TSetmealCheckgroupKey> tSetmealCheckgroupKeys = tSetmealCheckgroupMapper.selectByExample(tSetmealCheckgroupExample);

        ArrayList<TCheckgroup> checkgroups = new ArrayList<>();
        for (TSetmealCheckgroupKey tSetmealCheckgroupKey : tSetmealCheckgroupKeys) {
            TCheckgroup tCheckgroup = tCheckgroupMapper.selectByPrimaryKey(tSetmealCheckgroupKey.getCheckgroupId());
            checkgroups.add(tCheckgroup);
        }
        // 根据所有检查组去查询所有的检查项
        for (TCheckgroup tcheckgroup : checkgroups) {
            ArrayList<TCheckitem> checkitems = new ArrayList<>();
            TCheckgroupCheckitemExample tCheckgroupCheckitemExample = new TCheckgroupCheckitemExample();
            tCheckgroupCheckitemExample.createCriteria().andCheckgroupIdEqualTo(tcheckgroup.getId());
            List<TCheckgroupCheckitemKey> tCheckgroupCheckitemKeys = tCheckgroupCheckitemMapper.selectByExample(tCheckgroupCheckitemExample);
            for (TCheckgroupCheckitemKey tCheckgroupCheckitemKey : tCheckgroupCheckitemKeys) {
                TCheckitem tCheckitem = tCheckitemMapper.selectByPrimaryKey(tCheckgroupCheckitemKey.getCheckitemId());
                checkitems.add(tCheckitem);
            }
            tcheckgroup.setCheckItems(checkitems);
        }
        tSetmeal.setCheckgroups(checkgroups);
        return tSetmeal;
    }

//    public TSetmeal findById(Integer id) {
//        //根据套餐id查询套餐
//        TSetmeal tSetmeal = tSetmealMapper.selectByPrimaryKey(id);
//        //根据套餐id查询出所以的检查组
//        TSetmealCheckgroupExample tSetmealCheckgroupExample = new TSetmealCheckgroupExample();
//        tSetmealCheckgroupExample.createCriteria().andSetmealIdEqualTo(id);
//        //查询中间表
//        List<TSetmealCheckgroupKey> tSetmealCheckgroupKeys = tSetmealCheckgroupMapper.selectByExample(tSetmealCheckgroupExample);
//
//        ArrayList<TCheckgroup> checkgroups = new ArrayList<>();
//        for (TSetmealCheckgroupKey tSetmealCheckgroupKey : tSetmealCheckgroupKeys) {
//            TCheckgroup tCheckgroup = tCheckgroupMapper.selectByPrimaryKey(tSetmealCheckgroupKey.getCheckgroupId());
//            checkgroups.add(tCheckgroup);
//        }
//        //根据所以检查组去查询所以检查项
//        for (TCheckgroup tcheckgroup : checkgroups){
//            ArrayList<TCheckitem> checkitems = new ArrayList<>();
//            TCheckgroupCheckitemExample tCheckgroupCheckitemExample = new TCheckgroupCheckitemExample();
//            tCheckgroupCheckitemExample.createCriteria().andCheckgroupIdEqualTo(tcheckgroup.getId());
//            List<TCheckgroupCheckitemKey> tCheckgroupCheckitemKeys = tCheckgroupCheckitemMapper.selectByExample(tCheckgroupCheckitemExample);
//
//            for (TCheckgroupCheckitemKey tCheckgroupCheckitemKey : tCheckgroupCheckitemKeys) {
//                TCheckitem tCheckitem = tCheckitemMapper.selectByPrimaryKey(tCheckgroupCheckitemKey.getCheckitemId());
//                checkitems.add(tCheckitem);
//            }
//            tcheckgroup.setCheckItems(checkitems);
//        }
//        tSetmeal.setCheckgroups(checkgroups);
//        return tSetmeal;
//    }


    public static void main(String[] args) throws ClientException {
        SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,"18140479947","1314");
    }
}
